package me.xplabs.ui 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import me.xplabs.Globals;
	import me.xplabs.interfaces.IBaseView;
	
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class BaseView implements IBaseView
	{
		protected var _container:BaseSprite;
		private var _father:DisplayObjectContainer;
		private var _isInit:Boolean;
		private var _maskEnabled:Boolean;
		private var _maskShape:Shape;
		
		public var pw:int;
		public var ph:int;
		public function BaseView(pfather:DisplayObjectContainer) 
		{
			_father = pfather;
		}
		protected function init():void
		{
			_container = new BaseSprite();
		}
		
		public function addChild(p:DisplayObject):void
		{
			if (!_container.contains(p)) _container.addChild(p);
			else _container.setChildIndex(p, _container.numChildren - 1);
		}
		public function removeChild(p:DisplayObject):void
		{
			if (_container.contains(p)) _container.removeChild(p);
		}
		public function get width():Number
		{
			return pw;
		}
		public function get height():Number
		{
			return ph;
		}
		public function setXY(px:Number, py:Number):void
		{
			_container.x = px;
			_container.y = py;
			if (_maskEnabled)
			{
				_maskShape.x = -_container.x;
				_maskShape.y = -_container.y;
			}
		}
		public function show():void 
		{
			if (!_isInit)
			{
				init();
				_isInit = true;
			}
			if (!_container.parent) _father.addChild(_container);
		}
		public function get maskEnabled():Boolean
		{
			return _maskEnabled;
		}
		/**
		 * 设置面板的遮罩
		 *@param pval 是否支持遮罩
		 */
		public function set maskEnabled(pval:Boolean):void
		{
			if (_maskEnabled == pval) return;
			
			_maskEnabled = pval;
			if (_maskEnabled) {
				_maskShape = new Shape();
				_maskShape.graphics.beginFill(0x000000, .2);
				_maskShape.graphics.drawRect(0, 0, Globals.stageW, Globals.stageH);
				_maskShape.graphics.endFill();
				_maskShape.x = -_container.x;
				_maskShape.y = -_container.y;
				_container.addChildAt(_maskShape, 0);
			}
			else {
				if (_maskShape && _maskShape.parent) _maskShape.parent.removeChild(_maskShape);
				_maskShape = null;
			}
		}
		public function hide():void 
		{
			if (_father)
			{
				if (_father.parent) _father.parent.removeChild(_father);
			}
		}
		
		public function visible():Boolean 
		{
			if (!_father || !_father.parent)  return false;
			return true;
		}
		public function dispose():void
		{
			
		}
	}

}